package com.road.yishi.execaction;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CmdTask implements Runnable {

	private static final Logger LOG = LoggerFactory.getLogger(CmdTask.class);
	private CmdTaskQueue queue;
	private Command cmd;
	private String content;
	protected Long createTime;

	public CmdTask(CmdTaskQueue queue, String content) {
		super();
		this.queue = queue;
		this.content = content;
		this.createTime = System.currentTimeMillis();
	}

	@Override
	public void run() {
		if (queue != null) {
			long start = System.currentTimeMillis();
			try {
				cmd.execute(content);
				long end = System.currentTimeMillis();
				long interval = end - start;
				long leftTime = end - createTime;
				if (interval >= 1000) {
					LOG.warn("execute action : " + this.toString() + ", interval : " + interval + ", leftTime : "
							+ leftTime + ", size : " + queue.getQueue().size());
				}
			} catch (Exception e) {
				LOG.error("执行 command 异常, command : " + cmd.toString() + ", content : " + content, e);
			} finally {
				queue.dequeue(this);
			}
		}
	}

	@Override
	public String toString() {
		return cmd.toString();
	}
}
